package com.slmxz.tms.document.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.slmxz.tms.common.enums.BizMessageEnum;
import com.slmxz.tms.core.exception.Asserts;
import com.slmxz.tms.core.exception.BizException;
import com.slmxz.tms.common.result.ResultCode;
import com.slmxz.tms.document.mapper.SettleCurrencyMapper;
import com.slmxz.tms.document.model.entity.SettleCurrency;
import com.slmxz.tms.document.model.vo.SettleCurrencyVO;
import com.slmxz.tms.document.service.SettleCurrencyService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import cn.hutool.core.util.StrUtil;

/**
 * 结算币种汇率服务实现类
 * @author sunyao
 * @Date 2024-11-08
 */
@Service
@RequiredArgsConstructor
public class SettleCurrencyServiceImpl extends ServiceImpl<SettleCurrencyMapper, SettleCurrency> implements SettleCurrencyService {

    /**
    * 获取结算币种汇率分页列表
    * @return {@link IPage< SettleCurrencyVO >} 结算币种汇率分页列表
    */
    @Override
    public IPage<SettleCurrencyVO> selectSettleCurrencyPage(IPage<SettleCurrencyVO> page,SettleCurrencyVO settleCurrency) {
        return page.setRecords(this.baseMapper.selectSettleCurrencyPage(page,settleCurrency));
    }
    
    /**
     * @param id 结算币种汇率ID
     * @return
     */
    @Override
    public SettleCurrencyVO selectSettleCurrencyDetail(Long id) {
        SettleCurrency entity = this.getById(id);
        SettleCurrencyVO settleCurrency = BeanUtil.copyProperties(entity,SettleCurrencyVO.class);
        return settleCurrency;
    }
    
    /**
     * 新增结算币种汇率
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveSettleCurrency(SettleCurrency settleCurrency) {
        return this.save(settleCurrency);
    }
    
    /**
     * 更新结算币种汇率
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateSettleCurrency(SettleCurrency settleCurrency) {
        return this.updateById(settleCurrency);
    }
    
    /**
     * 删除结算币种汇率
     *
     * @param ids 结算币种汇率ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteSettleCurrencys(String ids) {
        Asserts.fail(StrUtil.isBlank(ids), BizMessageEnum.PARAM_IS_NULL);

        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean activeOrLogout(SettleCurrencyVO settleCurrency) {
        //激活或者注销业务数据
        Asserts.fail((CollectionUtil.isEmpty(settleCurrency.getIds()) || settleCurrency.getIsDeleted() == null),BizMessageEnum.PARAM_IS_NULL);

        List<SettleCurrency>  settleCurrencyList = new ArrayList<>(settleCurrency.getIds().size());
        settleCurrency.getIds().forEach(id -> {
            SettleCurrency settleCurrencyParam = new SettleCurrency();
            settleCurrencyParam.setId(id);
            settleCurrencyParam.setIsDeleted(settleCurrency.getIsDeleted());
            settleCurrencyList.add(settleCurrencyParam);
        });
        this.updateBatchById(settleCurrencyList,10);
        return true;
    }

}
